<template>
	<el-dialog :title="`确定将该记录设置为“${form.processTypeName}”？`" v-model="visible" :close-on-click-modal="false" draggable width="500">
		<el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
			<el-row :gutter="24">
				<el-col :span="22" class="mb20">
					<el-form-item label="备注" prop="remarks">
						<el-input v-model="form.remarks" placeholder="请输入备注,可留空" show-word-limit maxlength="100" />
					</el-form-item>
					<el-form-item label="设备状态" prop="deviceState">
						<div class="flex items-center">
							<el-checkbox v-model="form.deviceState" :true-label="20" :false-label="10">重置为"正常"</el-checkbox>
							<el-tooltip class="box-item" effect="dark" placement="top">
								<el-icon class="ml-1"><QuestionFilled /></el-icon>
								<template #content>
									<div class="w-[160px]">部分无恢复事件的设备（如红外）报警后，强制将设备状态重置为正常</div>
								</template>
							</el-tooltip>
						</div>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="visible = false">取消</el-button>
				<el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts" name="AlarmHandlingForm">
import { useMessage } from '/@/hooks/message';
import { handle } from '/@/api/alarm/alarmLogging';
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false);
const loading = ref(false);
// 定义字典

// 提交表单数据
const form = reactive({
	id: '',
	remarks: '',
	deviceState: 20,
});

// 定义校验规则
const dataRules = ref({
	// remarks: [{ required: true, message: '说明不能为空', trigger: 'blur' }],
});

// 打开弹窗
const openDialog = (id: string, row: any) => {
	visible.value = true;
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});
	Object.assign(form, row, { remarks: '', deviceState: 20, id: id });
	// 重置表单数据
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;

	try {
		loading.value = true;
		await handle({
			id: form.id,
			remarks: form.remarks,
			deviceState: form.deviceState,
			processTypeName: form.processTypeName,
			processTypeLevel: form.processTypeLevel,
		});
		useMessage().success('操作成功！');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
		loading.value = false;
	}
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
